<%@page import="com.mbbcpa.util.StringUtils"%>
<%@ page import = "java.io.*" %>
<%@ page import = "java.net.*" %>
<%@ page import = "java.text.*" %>
<%@ page import = "java.util.*" %>
<%@ page import = "java.util.HashMap" %>

<%@page import="com.mbbcpa.reference.RefListModel"%>
<%@ page import = "java.util.Map" %>
<%@ page import = "java.util.Vector" %>
<%@ page import="com.mbbcpa.app.manager.*" %>
<%@ page import="com.mbbcpa.app.EncryptionHelper" %>
<%@ page import="com.mbbcpa.app.EbppKeys" %>
<%@ page import="com.mbbcpa.util.*" %>
<%@ page import = "com.mbbcpa.security.dao.*" %>
<%@ page import = "com.mbbcpa.app.dao.*" %>
<%

// -- Load Constants
	Map map = null;
	Vector vCapacity = null;
	Vector vIdType = null;
	Vector vLoanInfo = null;
	Vector vBorrowerDetails = null;
	Vector vFacilityInfo = null;
	Vector vLoanSecurityExisting = null;
	LoanInfoDao loanInfoDao = null;
	
	DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
	Date date = new Date();
	String newDate = dateFormat.format(date);
	int loanSecurityCount = 0;
	int tblLoanSecurityCount = 0;
	int facilityCount = 0;
	int borrowerCount = 0;
	int loanSecurityIndex = 0; //for editing
	int facilityIndex = 0;

	boolean isNew = true;
	String sMessage = "";	
	String sAction = "";
	
	//form fields
	String dataForm = "loanSecurity";
	String securityID = "";
	String securityType = "";
	String securityValue = "";
	String loanSecurityDateTime = "";
	String facilityDateTimeId = "";

	
	// load combo box data from Map
	if (request.getAttribute("map") != null){
		
		map = (Map) request.getAttribute("map");
	
		
		//load data from main screen
		loanInfoDao = (LoanInfoDao) map.get("loanInfoDao");	
		vBorrowerDetails = (Vector) map.get("vBorrowerDetails");
		vFacilityInfo = (Vector) map.get("vFacilityInfo");
		facilityIndex = (Integer) map.get("facilityIndex"); //mother key	
		
		// Get Loan Security Details of the selected Facility Info
		FacilityInfoDao facilityInfoTbl = (FacilityInfoDao) vFacilityInfo.elementAt(facilityIndex);
		vLoanSecurityExisting = new Vector();
		vLoanSecurityExisting = facilityInfoTbl.getvLoanSecurity();
		tblLoanSecurityCount = vLoanSecurityExisting.size();		
		
		if (request.getAttribute("loanSecurityIndex") != null){
			
			isNew = false;
			loanSecurityIndex = (Integer) request.getAttribute("loanSecurityIndex");
			
			//get selected loanSecurity(index)
			LoanSecurityDao loanSecurityDaoEdit = (LoanSecurityDao) vLoanSecurityExisting.elementAt(loanSecurityIndex);
			
			 securityID = loanSecurityDaoEdit.getSecurityID();
			 securityType = loanSecurityDaoEdit.getSecurityType();
			 securityValue = loanSecurityDaoEdit.getSecurityValue();
			 loanSecurityDateTime = loanSecurityDaoEdit.getDateTime();
			 facilityDateTimeId = loanSecurityDaoEdit.getFacilityDateTimeId();
			
		}
		
		
		
		
/*  		if (vLoanSecurity != null){
			
			//edit flag checker
			if (request.getAttribute("elementIndex") != null){
				
				//set values for editing
				isNew = false;
				loanSecurityIndex = (Integer) request.getAttribute("elementIndex");
				LoanSecurityDao loanSecurityDaoEdit = (LoanSecurityDao) vLoanSecurity.elementAt(loanSecurityIndex);
				
				 securityID = loanSecurityDaoEdit.getSecurityID();
				 securityType = loanSecurityDaoEdit.getSecurityType();
				 securityValue = loanSecurityDaoEdit.getSecurityValue();
				 loanSecurityDateTime = loanSecurityDaoEdit.getDateTime();
				 facilityDateTimeId = loanSecurityDaoEdit.getFacilityDateTimeId();

	
			}
				loanSecurityCount =  Integer.parseInt(StringUtils.NullSafe(request.getParameter("loanSecurityCount")));
				tblLoanSecurityCount = loanSecurityCount; //add the count of data from the main screen
		} */
		
		//get other Data from mainScreen
		if (vBorrowerDetails != null){
			borrowerCount = Integer.parseInt(StringUtils.NullSafe(request.getParameter("borrowerCount")));
		}
 		
		if (vFacilityInfo != null){
			facilityCount = Integer.parseInt(StringUtils.NullSafe(request.getParameter("facilityCount")));
		}		
		
 		
		//Determine Screen details (if there is an index to be edited)
		if (request.getAttribute("loanSecurityIndex") != null){
 			sMessage = "Edit Loan Security";
			sAction = "Update Loan Security";
		}else{
			sMessage = "Add Loan Security (" + (tblLoanSecurityCount + 1) + ")";
			sAction = "Add Loan Security";
		}
	}
	


// -- Navigation

	// Get Manual CP Acces List
	ArrayList secGroupAccessList = (ArrayList) session.getAttribute(Constants.LOGON_ACCESS_LIST);
	
	// Get Login Details 
	SecUserDao oSecUserDao = (SecUserDao) session.getAttribute(Constants.LOGGED_USER);
	String sUserName = oSecUserDao.getUserName();
	String lastLogonDateTime = (String) session.getAttribute(Constants.LAST_LOGON_DATETIME);
	
	// Module Declaration
	int iMan001 = 0;
	int iMan002 = 0;
	
	for(int i=0;i<secGroupAccessList.size();i++){
		SecAccessDao secaccessDao = (SecAccessDao)secGroupAccessList.get(i);
		String strModuleCode  = secaccessDao.getModuleCode();
	
		if(strModuleCode.equals("MAN001"))
			iMan001 = 1;
		if(strModuleCode.equals("MAN002"))
			iMan002 = 1;
	}

	
	

%>

<html>
<head>
<link rel="stylesheet" href="resource/mbb_sec_global.css" type="text/css" />
<link href="resource/style.css" rel="stylesheet" type="text/css">

<script type="text/javascript">
	    window.history.forward();
	    function noBack() { window.history.forward(); }
	</script>
<style type="text/css"></style></head>

<SCRIPT LANGUAGE="JavaScript1.2">

	function validateLoanSecurityListing(){
		
		var isValid = false;
		var frm = document.frmFacilityInfo;
		
		//Validations Here
		if (frm.securityID.value == ""){
			alert("Please input Security ID");
			frm.securityId.focus();
		}else if(frm.securityType.value == ""){
			alert("Please input Security Type");
			frm.securityType.focus();
		}else if(frm.securityValue.value == ""){
			alert("Please input Security Value");
			frm.securityValue.focus();
		}else{
			isValid = true;
		}
		return isValid; 
	}	

	
	
	function addLoanSecurity(){
		var frm = document.frmFacilityInfo;
		//call validation function
		var isValid = validateLoanSecurityListing();
		
		//alert (isValid);
			//validation success? put loan info in vector
			if (isValid == true){
				frm.action = "ManualCPInquiry.do?dispatch=showManualCPMain";
				frm.submit();
			}
		
	};



</SCRIPT>


</head>


<body class="home" onload="noBack();" onpageshow="if (event.persisted) noBack();" onunload="">

	<a name="top"></a>
	
<!-- Header -->
<%@ include file="/jsp/common/Header.jsp" %>	
<!-- End Header -->

<div id="bgWrapper">
		<div id="container">
		
					<div id="titleBar">
						<ul class="topBar">
							<li class="goBackBt"><a href="<%=request.getContextPath()%>/CheckUserLogon.do?dispatch=homePage"></a></li>
							<li>
							  <h1>Manual CP Inquiry</h1></li>
						</ul>
					</div>
		
					<div id="fullcontent">
		
						<!-- Security Navigation -->
						<%@ include file="ManualCPNavigation.jsp" %>	
						<!-- End SecurityNavigation -->
			
			
						<div id="contentRightIn">
									
							<div id="moduleContainer">
							
							
									<!--  Module Title -->
									<table align="center" width=100% border="0" rules="cols" class="tblDetails"  style="margin-top:-.1%">
										<thead>
											<tr>
												<th colspan="4">
													<h4>Manual CP Inquiry</h4>
												</th>
											</tr>
										</thead>
									</table>

									<!-- Facility Info Form -->
									<table align="center" class="tblDetails" width=100% border="0" rules="cols">
										
										<form name="frmFacilityInfo" method="post">	
										
										<thead class="th1">
											<tr>
												<th colspan="4">
													<div class="frmTitle">
														<h2 class="manualCPFormTitle"><%=sMessage%></h2>
													</div>
												</th>
											</tr>
										</thead>
											
										<tbody>
											 <tr class="even">
										        <td width="25%">Security ID:</td>
										        <td width="25%">
													<% if (isNew == true){ %>
										       			<input name="securityID" type="text" />
										        	<% }else{ %>
										        		<input name="securityID" type="text" value="<%=securityID%>" />
										        	<% } %>
										        </td>
										        <td width="25%">Security Value:</td>
										        <td width="25%">
													<% if (isNew == true){ %>
										       			<input name="securityValue" type="text" />
										        	<% }else{ %>
										        		<input name="securityValue" type="text" value="<%=securityValue%>" />
										        	<% } %>
										        </td>
										      </tr>
										      
										      <tr class="odd">
										        <td width="25%">Security Type:</td>
										        <td width="25%">											
													<% if (isNew == true){ %>
										       			<input name="securityType" type="text" />
										        	<% }else{ %>
										        		<input name="securityType" type="text" value="<%=securityType%>" />
										        	<% } %>					        	
										        </td>
										        
										        <td width="25%"></td>
										        <td width="25%">
										        	<!-- FacilityDateTimeId - Mother Key of Loan Security -->
										        	<input name="facilityIndex" type="hidden" value="<%=facilityIndex%>" />
										        		
										        	<!-- LoanInfoDateTime -->
											        <% if (isNew == true){ %>
											      		<input name="loanSecurityDateTime" type="hidden" value="<%=newDate%>" />
											      	 <% }else{ %>
											      		<input name="loanSecurityDateTime" type="hidden" value="<%=loanSecurityDateTime%>" />
											      	 <% } %>		        	
										       
										        
										        </td>
										        
										      </tr>  
										      

										      

										      
										      <tr class="even">
										      	<td></td>
										      	<td></td>
										      	<td></td>
										        <td width="100%">
										       	
										        	<!-- DataForm -->
										        	<input type="hidden" name="dataForm" value="<%=dataForm%>" />
										        	<!-- Loan Info -->
										        	<input type="hidden" name="countryCode" value="<%=loanInfoDao.getCountryCode()%>" />
										        	<input type="hidden" name="finType" value="<%=loanInfoDao.getEntity()%>" />
										        	<input type="hidden" name="aaNo" value="<%=loanInfoDao.getAANo()%>" />
										        	<input type="hidden" name="losSrc" value="<%=loanInfoDao.getLOSSrc()%>" />
										        
										        	<input class="inputButton" type="button" name="btnAddLoanSecurity" value="<%=sAction%>" onClick="addLoanSecurity();">
												</td>
										      </tr> 											
										</tbody>
											
									</table>
										
									
									<!-- Loan Security Listing (Load all the loan security listing of the selected facilityInfo) -->
									<table align="center" style="display:block;" class="tblDetails" width=100% border="0" rules="cols">
														<thead class="th1">
															<tr>
																<th colspan="10">
																	<div class="frmTitle">
																		<h2 class="manualCPFormTitle">Loan Security Listing</h2>
																	</div>
																</th>
															</tr>
														</thead>
											
														<tbody>
															<!-- Detail Titles -->
														    <tr class="headerD" style="text-align:center !important;">
																<td width="5%" class="center_note">No.</td>
																<td width="15%" class="center_note">Security ID</td>
																<td width="15%" class="center_note">Security Type</td>
																<td width="15%" class="center_note">Security Value</td>
															</tr>
															
															
															<!-- LoanSecurity Rows -->
															<%
															if (vLoanSecurityExisting != null){
																tblLoanSecurityCount = vLoanSecurityExisting.size();
															}
															
															if (tblLoanSecurityCount > 0){
																
																if (isNew == false){
																	vLoanSecurityExisting.remove(loanSecurityIndex); //remove object that is being edited
																	tblLoanSecurityCount--; //decrease count by 1 because of the object being edited
																}
																
																 for(int a = 0; a < vLoanSecurityExisting.size(); a++){
																	LoanSecurityDao loanSecurityDao = (LoanSecurityDao) vLoanSecurityExisting.elementAt(a);
																	String rowclass3 = ((a % 2) == 0 ? "odd":"even");		
															%>	
																	
																<tr class="<%=rowclass3%>">										
																	<td class="center_note"><input name="facilityIndex" type="hidden" value="<%=loanSecurityDao.getFacilityIndex()%>" />
																	<input name="loanIndex" type="hidden" value="<%=a%>" />  <%=a+1%></td>
																	<td class="center_note"><input name=<%="securityId" + (a + 1) %> type="hidden" value="<%=loanSecurityDao.getSecurityID()%>" /><%=loanSecurityDao.getSecurityID()%></td>
																	<td class="center_note"><input name="<%="securityType" + (a + 1) %>" type="hidden" value="<%=loanSecurityDao.getSecurityType()%>" /><%=loanSecurityDao.getSecurityType()%></td>
																	<td class="center_note"><input name="<%="securityValue" + (a + 1) %>" type="hidden" value="<%=loanSecurityDao.getSecurityValue()%>" /><%=loanSecurityDao.getSecurityValue()%></td>
																</tr>
															<% } // for loop (loanSecurity)  
																
															
																// add the entry data that will be sent from the form
																tblLoanSecurityCount = tblLoanSecurityCount + 1;
															}//ifElse tblLoanSecurityCount
															
															
															%>
															  
														</tbody>
									</table>
									
									
									
									
												
											<!-- Other Hidden Table -->
													
													<!-- FacilityInfo Rows -->
													<table align="center" style="display:none;" class="tblDetails" width=100% border="0" rules="cols">
														<thead class="th1">
															<tr>
																<th colspan="10">
																	<div class="frmTitle">
																		<h2 class="manualCPFormTitle">Facility Info Listing</h2>
																	</div>
																</th>
															</tr>
														</thead>
											
														<tbody>
															<!-- Detail Titles -->
														    <tr class="headerD" style="text-align:center !important;">
																<td width="5%" class="center_note">No.</td>
																<td width="15%" class="center_note">Facility ID</td>
																<td width="15%" class="center_note">Facility Type Code</td>
																<td width="15%" class="center_note">Product Type Code</td>
																<td width="15%" class="center_note">Purpose of Loan</td>
																<td width="15%" class="center_note">Loan Amount</td>
																<td width="15%" class="center_note">Branch Code</td>
																<td width="15%" class="center_note">Loan Tenure</td>
															</tr>  
														
														<!-- FacilityInfo Rows -->
															<% 
																if (facilityCount > 0 ){
																	for(int j = 0; j < vFacilityInfo.size(); j++){
																		FacilityInfoDao facilityInfo = (FacilityInfoDao) vFacilityInfo.elementAt(j);
																		String rowclass = ((j % 2) == 0 ? "even":"odd");											
															
																		//Get Loan Security Details
																		Vector vLoanSecurity = new Vector();
																		loanSecurityCount = 0;
																		vLoanSecurity = facilityInfo.getvLoanSecurity();
																		if (vLoanSecurity != null){
																			loanSecurityCount = vLoanSecurity.size();
																		}
																		
/* 																		if (isNew == false){
																			vLoanSecurity.remove(loanSecurityIndex); //remove object that is being edited
																			loanSecurityCount--; //decrease count by 1 because of the object being edited
																		} */
																		
															%>
																	<tr class="<%=rowclass%>">
																	  
																		<td><input name="facilityIndex" type="hidden" value="<%=j%>" />  <%=j+1%></td>
																		<td><input name="<%="facilityId" + (j + 1) %>" type="hidden" value="<%=facilityInfo.getFacId()%>" /><%=facilityInfo.getFacId()%></td>
																		<td><input name="<%="facilityTypeCode" + (j + 1) %>" type="hidden" value="<%=facilityInfo.getFacTypeCode()%>" /><%=facilityInfo.getFacId()%></td>
																		<td><input name="<%="productTypeCode" + (j + 1) %>" type="hidden" value="<%=facilityInfo.getFacProdCode()%>" /><%=facilityInfo.getFacProdCode()%></td>
																		<td><input name="<%="purposeOfLoan" + (j + 1) %>" type="hidden" value="<%=facilityInfo.getFacPOL()%>" /><%=facilityInfo.getFacPOL()%></td>
																		<td><input name="<%="loanAmount" + (j + 1) %>" type="hidden" value="<%=facilityInfo.getLoanAmount()%>" /><%=facilityInfo.getLoanAmount()%></td>
																		<td><input name="<%="branchCode" + (j + 1) %>" type="hidden" value="<%=facilityInfo.getBranchCode()%>" /><%=facilityInfo.getBranchCode()%></td>
																		<td><input name="<%="loanTenure" + (j + 1) %>" type="hidden" value="<%=facilityInfo.getLoanTenure()%>" /><%=facilityInfo.getLoanTenure()%>
																		<input name="<%="facilityDateTime" + (j + 1) %>" type="hidden" value="<%=facilityInfo.getDateTime()%>" />
																		<input name="<%="loanSecurityCount" + (j + 1) %>" type="hidden" value="<%=loanSecurityCount%>" /></td>
														
																
																	</tr>
																	
																	
															<!-- Loan Security Data -->
															<% if (loanSecurityCount > 0){ %>
															<tr class="<%=rowclass%>">
																<td colspan="10">
																	<table align="center" style="float:right;display:none;margin-top:-1%;width:98% !important;" class="tblDetails" width=95% border="0" rules="cols">
																		<thead class="th1">
																			<tr>
																				<th colspan="10">
																					<div class="frmTitle">
																						<h2 class="manualCPFormTitle">Loan Security Listing</h2>
															  						</div>
																				</th>
																			</tr>
																		</thead>
											
																		<tbody>
																			<!-- Detail Titles -->
																		    <tr class="headerD" style="text-align:center !important;">
																				<td width="5%" class="center_note">No.</td>
																				<td width="15%" class="center_note">Security ID</td>
																				<td width="15%" class="center_note">Security Type</td>
																				<td width="15%" class="center_note">Security Value</td>
																				<td width="15%" class="center_note" colspan="2" class="center_note">Action</td>
																			</tr>  
																		
																		<% for(int k = 0; k < vLoanSecurity.size(); k++){
																				LoanSecurityDao loanSecurityDao = (LoanSecurityDao) vLoanSecurity.elementAt(k);
																				String rowclass2 = ((k % 2) == 0 ? "odd":"even");		
																		%>	
																				
																			<tr class="<%=rowclass%>">										
																				<td class="center_note"><input name="facilityIndex" type="hidden" value="<%=loanSecurityDao.getFacilityIndex()%>" />
																				<input name="loanIndex" type="hidden" value="<%=k%>" />  <%=k+1%></td>
																				<td class="center_note"><input name="<%=j + "_securityId" + (k + 1) %>" type="hidden" value="<%=loanSecurityDao.getSecurityID()%>" /><%=loanSecurityDao.getSecurityID()%></td>
																				<td class="center_note"><input name="<%=j + "_securityType" + (k + 1) %>" type="hidden" value="<%=loanSecurityDao.getSecurityType()%>" /><%=loanSecurityDao.getSecurityType()%></td>
																				<td class="center_note"><input name="<%=j + "_securityValue" + (k + 1) %>" type="hidden" value="<%=loanSecurityDao.getSecurityValue()%>" /><%=loanSecurityDao.getSecurityValue()%>
																				<input name="<%=j + "_loanSecurityDateTime" + (k + 1) %>" type="hidden" value="<%=loanSecurityDao.getDateTime()%>" /><%=loanSecurityDao.getDateTime()%></td>
																			</tr>
																		<% } // for loop (loanSecurity)  %>
																		
																		</tbody>
																	</table>
																</td></tr> <!-- ending tag -->
																<% }//if else (loanSecurityCount) 
																	//loanSecurityCount = 0; //reset count for loop
																}//for loop (vFacilityInfo)
															
															}//if else (facilityCount)
															%>
														</tbody>
													</table>
													
													<!-- Borrower Details -->
													<table align="center" style="display:none;" class="tblDetails" width=100% border="0" rules="cols">
													<thead class="th1">
														<tr>
															<th colspan="9">
																<div class="frmTitle">
																	<h2 class="manualCPFormTitle">Loan Borrower Listing</h2>
																</div>
															</th>
														</tr>
													</thead>
											
													<tbody>
														<!-- Detail Titles -->
													    <tr class="headerD" style="text-align:center !important;">
															<td width="5%" class="center_note">No.</td>
															<td width="7%" class="center_note">GCIF No.</td>
															<td width="14%" class="center_note">Capacity</td>
															<td width="14%" class="center_note">ID Type</td>
															<td width="14%" class="center_note">ID No.</td>
															<td width="14%" class="center_note">Other ID</td>
															<td width="14%" class="center_note">Name</td>
															<td width="14%" class="center_note">Percentage of Share</td>
														</tr>  
													
														
														<!-- Detail Rows -->
															<% 
														
															if (borrowerCount > 0){
																for(int j = 0; j < vBorrowerDetails.size(); j++){
																
																	BorrowerDetailsDao borrowerDetails = (BorrowerDetailsDao) vBorrowerDetails.elementAt(j);	
																	String rowclass = ((j % 2) == 0 ? "even":"odd");	
		
															%>
															
																<tr class="<%=rowclass%>">
																	<td class="center_note"><%=j+1%></td>
																	<td><input name="<%="gcifNo" + (j + 1) %>" type="hidden"   value="<%=borrowerDetails.getGCIFNo()%>" /><%=borrowerDetails.getGCIFNo()%></td>
																	<td><input name="<%="capacity" + (j + 1) %>" type="hidden"   value="<%=borrowerDetails.getCapacity()%>" /><%=borrowerDetails.getCapacity()%></td>
																	<td><input name="<%="idType" + (j + 1) %>" type="hidden"   value="<%=borrowerDetails.getIDType()%>" /><%=borrowerDetails.getIDType()%></td>
																	<td><input name="<%="idNo" + (j + 1) %>" type="hidden"   value="<%=borrowerDetails.getIDNo()%>" /><%=borrowerDetails.getIDNo()%></td>
																	<td><input name="<%="otherIdType" + (j + 1) %>" type="hidden"   value="<%=borrowerDetails.getOtherIDType()%>" />
																	<input name="<%="otherIdNo" + (j + 1) %>" type="hidden"   value="<%=borrowerDetails.getOtherID()%>" /><%=borrowerDetails.getOtherID()%></td>
																	<td><input name="<%="name" + (j + 1) %>" type="hidden"    value="<%=borrowerDetails.getName()%>" /><%=borrowerDetails.getName()%></td>
																	<td><input name="<%="percentage" + (j + 1) %>" type="hidden"   value="<%=borrowerDetails.getPercentageOfShare()%>" /><%=borrowerDetails.getPercentageOfShare()%>
																	<input name="<%="borrowerDateTime" + (j + 1) %>" type="hidden" value="<%=borrowerDetails.getDateTime()%>" /></td>
																</tr>	
															<%
																	
																}
																
															}
															%>
														</tbody>
														</table>
														
														<!-- Data Table Count -->			
								  						<input name="loanSecurityCount" type="hidden" value="<%=tblLoanSecurityCount%>" />
														<input name="tblLoanSecurityCount" type="hidden" value="<%=tblLoanSecurityCount%>" />
											       		<input name="facilityCount" type="hidden" value="<%=facilityCount%>" />
														<input name="borrowerCount" type="hidden" value="<%=borrowerCount%>" />
												</form>
														
									
									
						</div> <!--  moduleContainer END -->
						
					</div>
		
					<!-- Footer -->
					<%@ include file="/jsp/common/Footer.jsp" %>	
					<!-- End Footer -->
					</div>




</body>
</html>